<?php
class zyController extends AController {
	/**
	 * 返回住院历史信息
	 */
	public function historyAction() {
		$pid = $this->request->getGetParameter('pid');
		$page = $this->request->getGetParameter('p', 1);
		$pageSize = 4;
		
		$sql = "SELECT P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE,ZY.ZYDOC_NO,ZY.CY_DEP_NO,
V.VISIT_STATE, V.VISIT_RESULT, V.LAST_VISIT_TIME, V.VISIT_COUNT
FROM PATIENT P 
LEFT JOIN ZHUYUAN ZY ON P.PATIENT_ID=ZY.REF_PATIENT_ID 
LEFT JOIN LAST_VISIT V ON ZY.BIZ_CODE = V.BIZ_CODE 
WHERE P.PATIENT_ID={$pid} 
GROUP BY P.TRUE_NAME, P.PHONE, P.BIRTH, P.SEX, ZY.RY_DATE, ZY.CY_DATE, ZY.CY_DEP, ZY.ZYDOC, ZY_AGE, ZY.BIZ_CODE, ZY.ZYDOC_NO,ZY.CY_DEP_NO,V.VISIT_STATE, V.VISIT_RESULT, V.LAST_VISIT_TIME, V.VISIT_COUNT
ORDER BY ZY.CY_DATE DESC";
		
		$totalCount = 0;
		
		$this->rows = JYDB::query($sql, $page, $pageSize, $totalCount);
		$this->pager = JYTools::pager("index.php?c=zy&a=history&pid={$pid}&p=_num_", $totalCount, $page, $pageSize);
		return $this->render();
	}
	
	// 返回某次住院的详细信息
	public function detailAction() {
		$bizCode = $this->request->getGetParameter('bizcode');
		
		$zhuyuan = JYDB::find('ZHUYUAN', "BIZ_CODE='{$bizCode}'");
		
		if (!$zhuyuan) {
			return $this->alert('找不到患者资料');
		}
		
		// 病人最近的手术信息
		if ($zhuyuan['IS_OPERATE']) {
			$this->operation = JYDB::findAll('OPERATION', "ZY_NO='{$zhuyuan['ZY_NO']}' AND ZY_TIMES='{$zhuyuan['ZY_TIMES']}'", '*', 'OP_TIMES DESC');
		}
		
		$this->patient = JYDB::find('PATIENT', "PATIENT_ID={$zhuyuan['REF_PATIENT_ID']}", "TRUE_NAME, SEX");
		$this->zhuyuan = $zhuyuan;
		
		return $this->render();
	}
}